package com.fund.datacopy.conf;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.fund.datacopy.bean.Group;

public class Configure {
	private static final Logger logger = Logger.getLogger(Configure.class);

	private static Map<String, Group> map = new HashMap<String, Group>();

	public static void init() throws Exception {
		Set<Object> keys = ConfigureManger.getAllKeys();
		if (CollectionUtils.isEmpty(keys)) {
			return;
		}

		Iterator<Object> it = keys.iterator();
		while (it.hasNext()) {
			String key = it.next().toString();
			parseGroup(key);
		}
	}

	public static Collection<Group> getAllSettings() {
		return map.values();
	}

	private static void parseGroup(String log) throws Exception {
		Group group = null;
		String name = null;
		if (StringUtils.isBlank(log)) {
			return;
		}
		Matcher m = namePattern.matcher(log);
		if (m.find()) {
			name = m.group(1);
			if (map.containsKey(name)) {
				group = (Group) map.get(name);
			} else {
				group = new Group();
				group.setName(name);
			}
		} else {
			logger.error("group name is null " + log);
			throw new Exception("unvalidate group name setting " + log);
		}
		m = srcPattern.matcher(log);
		if (m.find()) {
			group.setSrc(ConfigureManger.getAsString(log));
		}
		m = destPattern.matcher(log);
		if (m.find()) {
			group.setDest(ConfigureManger.getAsString(log));
		}
		m = dbPattern.matcher(log);
		if (m.find()) {
			group.setDb(ConfigureManger.getAsString(log));
		}

		m = tablesPattern.matcher(log);
		if (m.find()) {
			String tables = ConfigureManger.getAsString(log);
			if (StringUtils.isBlank(tables)) {
				logger.error("table is null " + log);
				throw new Exception("unvalidate table setting " + log + " ");
			}
			String[] tabs = tables.split(Constants.COMMA);
			List<String> varTab = new ArrayList();
			List<String> varCol = new ArrayList();
			String[] arrayOfString1;
			int j = (arrayOfString1 = tabs).length;
			for (int i = 0; i < j; i++) {
				String tab = arrayOfString1[i];
				m = tabValPattern.matcher(tab);
				if (m.find()) {
					varTab.add(m.group(1));
					varCol.add(m.group(2));
				} else {
					varTab.add(tab);
					varCol.add("");
				}
			}
			group.setTable(varTab);
			group.setCollum(varCol);
		}

		m = stylePattern.matcher(log);
		if (m.find()) {
			String style = ConfigureManger.getAsString(log);
			if (StringUtils.isNotBlank(style)) {
				List<String> styles = new ArrayList();
				styles.addAll(Arrays.asList(style.split(Constants.COMMA)));
				group.setStyle(styles);
			}
		}

		m = extPattern.matcher(log);
		if (m.find()) {
			String ext = ConfigureManger.getAsString(log);
			if (StringUtils.isNotBlank(ext)) {
				List<String> exts = new ArrayList();
				m = extValPattern.matcher(ext);
				if (m.find()) {
					while (m.find()) {
						exts.add(m.group(1));
					}
				}
				group.setExt(exts);
			}
		}
		map.put(group.getName(), group);
	}

	public static void test(String log) {
		if (StringUtils.isBlank(log)) {
			return;
		}

		Matcher m = srcPattern.matcher(log);
		if (m.find()) {
			System.out.println(m.group(0));
		}

		m = destPattern.matcher(log);
		if (m.find()) {
			System.out.println(m.group(0));
		}

		m = dbPattern.matcher(log);
		if (m.find()) {
			System.out.println(m.group(0));
		}

		m = tablesPattern.matcher(log);
		if (m.find()) {
			System.out.println(m.group(0));
		}

		m = stylePattern.matcher(log);
		if (m.find()) {
			System.out.println(m.group(0));
		}

		m = extPattern.matcher(log);
		if (m.find()) {
			System.out.println(m.group(0));
		}

		m = tabValPattern.matcher(log);
		if (m.find()) {
			System.out.println(m.group(1) + " " + m.group(2));
		}

		m = extValPattern.matcher(log);
		if (m.find()) {
			while (m.find()) {
				System.out.println(m.group(1));
			}
		}
	}

	public static void main(String[] args) {
		test("group[fund].ext=group[fund].ext=[003166_OT_FD,003166_OT_FD,003166_OT_FD],[003166_OT_FD,003166_OT_FD],[003166_OT_FD],");
	}

	private static final Pattern namePattern = Pattern.compile("\\[(.*?)]");
	private static final Pattern srcPattern = Pattern.compile("group\\[(.*?)\\].src.db.url");
	private static final Pattern destPattern = Pattern.compile("group\\[(.*?)\\].dest.db.url");
	private static final Pattern dbPattern = Pattern.compile("group\\[(.*?)\\].db");
	private static final Pattern tablesPattern = Pattern.compile("group\\[(.*?)\\].tables");
	private static final Pattern stylePattern = Pattern.compile("group\\[(.*?)\\].style");
	private static final Pattern extPattern = Pattern.compile("group\\[(.*?)\\].ext");
	private static final Pattern tabValPattern = Pattern.compile("(.*?)\\[(.*?)\\]");
	private static final Pattern extValPattern = Pattern.compile("\\[(.*?)\\]");
}
